import { Component, OnInit, OnDestroy, OnChanges, Input, Output, EventEmitter } from '@angular/core';

import { DashPieComponentService } from './pie.component.service';

import { AppSettings } from '../../../app.settings';

import { Util } from '../../../utils/util';

import { Scope } from '../../../utils/emit';

import { ToastyService, ToastOptions } from '../../../modules/ng2-toasty';

@Component({
  selector: 'dashborad-pie',
  templateUrl: './pie.component.html',
  styleUrls: ['./pie.component.css'],
  providers: [DashPieComponentService]
})
export class DashboradPie implements OnInit {

  // ng-chart
  vm = {
    "tool": false,
    "loading": true,
    "fileName":'',
    "data": [],
    "type": 'pie'
  };

  columns = ['类目', '日销售额', '日销售量', '近30天销售额', '近30天销售量', '动销店铺数', '动销宝贝数'];

  constructor(private _service: DashPieComponentService, private appSetting: AppSettings, private util: Util, private scope: Scope,private toastyService:ToastyService) {

  }
  // 类目 接收pie component cat change
  cat: any = {
    catName: "",
    wholeName: "",
    categoryId: ""
  };
  eventOff:any;
  ngOnInit() {

    console.log('get us/////');

    console.log(this.appSetting.getAppCat());

    // 如果不是第一次进入
    this.cat = this.appSetting.getAppCat();

    if(this.cat.categoryId!=''){
      this.vm.loading = true;
      this.getHttp(this.cat);
    }


    this.eventOff = this.scope.on('dashborad-all-cur-cat', (data) => {
      console.log(data);
      this.cat = data.data;

      if(this.cat.categoryId!=''){
        this.vm.loading = true;
        this.getHttp(this.cat);
      }

    });

  }


  ngOnDestroy() {

    // this.util.sessionStore().set('dashPieData', { "store": { "arry": this.store.arry, "root": this.store.root }, "vm": { "tool": this.vm.tool, "isLeaf": this.vm.isLeaf } });

    if(this.offEvent){
      this.offEvent.unsubscribe();
    }

    if(this.eventOff){
      this.eventOff.unsubscribe();
    }
  }

  // excel export
  export(){

    let dim = ['date', 'categoryName', 'categoryId', 'sellMoney', 'sellAmount', 'monthSellMoney', 'monthSellAmount', 'productNum', 'shopNum', 'tsellMoney', 'tsellAmount', 'monthTSellMoney', 'monthTSellAmount', 'tproductNum', 'tshopNum', 'thksellMoney', 'thksellAmount', 'monthThkSellMoney', 'monthThkSellAmount', 'thkproductNum', 'thkshopNum', 'gsellMoney', 'gsellAmount', 'monthGSellMoney', 'monthGSellAmount', 'gproductNum', 'gshopNum', 'csellMoney', 'csellAmount', 'monthCSellMoney', 'monthCSellAmount', 'cproductNum', 'cshopNum'];

    let dimText = ['时间', '类目', '类目id', '日销售额', '日销售量', '月销售额', '月销售量', '宝贝数', '店铺数', '天猫日销售额', '天猫日销售量', '天猫月销售额', '天猫月销售量', '天猫宝贝数', '天猫店铺数', '天猫国际日销售额', '天猫国际日销售量', '天猫国际月销售额', '天猫国际月销售量', '天猫国际宝贝数', '天猫国际店铺数', '全球购日销售额', '全球购日销售量', '全球购月销售额', '全球购月销售量', '全球购宝贝数', '全球购店铺数', '淘宝日销售额', '淘宝日销售量', '淘宝月销售额', '淘宝月销售量', '淘宝宝贝数', '淘宝店铺数'];

    let data = this.util.jsonToArrry(this.vm.data, dim, dimText);


    this.util.export(data, this.vm.fileName);
  }

  // echarts图表转化
  typeChange(_type) {
    if (this.vm.type == _type) {
      return;
    }

    if (this.vm.loading) {
      return;
    }

    this.vm.type = _type;

    this.vm.loading = true;
    setTimeout(()=>{
      this.vm.loading = false;
    },500);


  }

  sellResulet:any = {
    sellAmount:0,
    sellMoney:0,
    monthSellAmount:0,
    monthSellMoney:0,
    shopNum:0,
    productNum:0
  };

  // 获取ajax
  offEvent:any;
  getHttp(_cat) {




    // this.cat = _cat;

    this.vm.fileName = this.cat.wholeName;

    this.sellResulet = {
      sellAmount:0,
      sellMoney:0,
      monthSellAmount:0,
      monthSellMoney:0,
      shopNum:0,
      productNum:0
    };

    var res = {"code":0,"msg":"成功","data":[{"id":"50019780\u0001","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","sellAmount":5703,"tsellAmount":4026,"csellAmount":1574,"thksellAmount":0,"gsellAmount":103,"monthSellAmount":254425,"monthTSellAmount":210365,"monthCSellAmount":41924,"monthThkSellAmount":15,"monthGSellAmount":2121,"sellMoney":7868591.29,"tsellMoney":4316238.0,"csellMoney":3308763.23,"thksellMoney":0.0,"gsellMoney":243590.06,"monthSellMoney":4.1179106436E8,"monthTSellMoney":3.131679927E8,"monthCSellMoney":9.326285549E7,"monthThkSellMoney":5520.0,"monthGSellMoney":5354696.17,"productNum":3028,"tproductNum":1090,"cproductNum":1908,"thkproductNum":1,"gproductNum":29,"shopNum":708,"tshopNum":178,"cshopNum":517,"thkshopNum":1,"gshopNum":12}]};

      this.sellResulet = this.util.getSellAllResult(this.sellResulet,res.data);

      if(res.data.length == 0){
        // toast
        let toastOptions: ToastOptions = {
          title: "",
          msg: "该类目暂无数据",
          showClose: true,
          timeout: 6000,
          theme: 'material'
        };
        // Add see all possible types in one shot
        this.toastyService.info(toastOptions);
        return;
      }

      // console.log(res);
      let data:any = [];
      if(res.data.length>1){
        data= res.data.sort((a, b) => {

          if (this.appSetting.sellTime.type == "month") {
            return b.monthSellMoney - a.monthSellMoney;
          } else {
            return b.sellMoney - a.sellMoney;
          }

        });
      }else{
        data = res.data;
      }

      if(Number(this.cat.categoryId)>0){
        data.isLeaf = true;
      }
      setTimeout(()=>{
        // pie echarts
      this.vm.loading = false;


      this.vm.data = data;

      },100);




   
  }



}
